package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.zhenhuan.UserActivityWorshipData;
import com.yanqu.road.entity.activity.zhenhuan.firstProcess.UserZhenHuanFirstProcessData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserActivityWorshipDaoImpl extends TempDataObjectDao<UserActivityWorshipData> {

    public static String getUserActivityWorshipDataSql = "select * from `t_u_activity_worship` where `userId`=?";

    public List<UserActivityWorshipData> getUserActivityWorshipData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<UserActivityWorshipData> dataList = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserActivityWorshipDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserActivityWorshipData data = parse(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public static String updateUserActivityWorshipDataSql = "update `t_u_activity_worship` set isWorship=?,worshipTime=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(UserActivityWorshipData userActivityWorshipData) {
        userActivityWorshipData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userActivityWorshipData.getIsWorship()));
        para.put(i++, new DbParameter(Types.BIGINT, userActivityWorshipData.getWorshipTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userActivityWorshipData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userActivityWorshipData.getUserId()));
        if (getDb().executeNoneQuery(updateUserActivityWorshipDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserActivityWorshipDataSql = "insert into `t_u_activity_worship`(activityId,userId,isWorship,worshipTime) values (?,?,?,?)";

    @Override
    protected boolean addData(UserActivityWorshipData userActivityWorshipData) {
        userActivityWorshipData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userActivityWorshipData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userActivityWorshipData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userActivityWorshipData.getIsWorship()));
        para.put(i++, new DbParameter(Types.BIGINT, userActivityWorshipData.getWorshipTime()));
        if (getDb().executeNoneQuery(insertUserActivityWorshipDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserActivityWorshipData parseData(ResultSet rs) throws SQLException {
        UserActivityWorshipData userActivityWorshipData = new UserActivityWorshipData();
        userActivityWorshipData.setActivityId(rs.getInt("activityId"));
        userActivityWorshipData.setUserId(rs.getLong("userId"));
        userActivityWorshipData.setIsWorship(rs.getInt("isWorship"));
        userActivityWorshipData.setWorshipTime(rs.getLong("worshipTime"));

        return userActivityWorshipData;
    }

}
